package com.itheima.JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.获取驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取链接
        String url = "jdbc:mysql://127.0.0.1:3306/db01";
        String username = "root";
        String password = "328802";
        Connection connection = DriverManager.getConnection(url, username, password);

        Statement statement = connection.createStatement();
        try {
            connection.setAutoCommit(false);
            String sql = "update account set money = money - 500 where id = 1";
            //4.获取执行sql的对象
            //5.执行sql
            int i = statement.executeUpdate(sql);
            //6.处理结果
            System.out.println(i);

            String sql2 = "update account set money = money +500 where id = 2";
            int i1 = statement.executeUpdate(sql2);
            System.out.println(i1);
            connection.commit();

        } catch (SQLException e) {
            connection.rollback();
            throw new RuntimeException(e);

        }        //3.定义sql

        //7.释放资源
        statement.close();
        connection.close();
    }
}
